package flc.ast.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.ToastUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.databinding.DialogSettingBinding;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.TimeUtil;

public class SettingDialog extends BaseSmartDialog<DialogSettingBinding> implements View.OnClickListener {

    private boolean hasOpenFlash;
    private boolean hasOpenVibrate;
    private boolean hasSetTime;
    //调速设置
    private int timeInterval;//时间间隔
    private int addSpeed;//每次增加bpm
    private int startSpeed;//开始速度
    private int maxSpeed;//最大速度
    private List<Integer> minuteList;
    private List<String> minuteStrList;
    private List<Integer> secondList;
    private List<String> secondStrList;

    public SettingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    public void setFlashLamp(boolean openFlash) {
        hasOpenFlash = openFlash;
    }

    public void setVibration(boolean openVibrate) {
        hasOpenVibrate = openVibrate;
    }

    public void setTime(boolean setTime) {
        hasSetTime = setTime;
    }

    public void setTimeInterval(int timeInterval) {
        this.timeInterval = timeInterval;
    }

    public void setAddSpeed(int addSpeed) {
        this.addSpeed = addSpeed;
    }

    public void setStartSpeed(int startSpeed) {
        this.startSpeed = startSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_setting;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvSetFlashLamp.setSelected(hasOpenFlash);
        mDataBinding.tvSetVibration.setSelected(hasOpenVibrate);
        mDataBinding.tvSetTime.setSelected(hasSetTime);
        mDataBinding.tvMinute.setText(TimeUtil.getHHmmss(timeInterval * 60 * 1000));
        mDataBinding.tvSpeed.setText(addSpeed + "");
        mDataBinding.tvStartSpeed.setText(startSpeed + "");
        mDataBinding.tvMaxSpeed.setText(maxSpeed + "");

        mDataBinding.tvSettingCancel.setOnClickListener(this);
        mDataBinding.tvSettingConfirm.setOnClickListener(this);

        mDataBinding.tvSetFlashLamp.setOnClickListener(this);
        mDataBinding.tvSetVibration.setOnClickListener(this);
        mDataBinding.tvSetTime.setOnClickListener(this);

        mDataBinding.ivMinuteReduce.setOnClickListener(this);
        mDataBinding.ivMinuteAdd.setOnClickListener(this);
        mDataBinding.ivSpeedReduce.setOnClickListener(this);
        mDataBinding.ivSpeedAdd.setOnClickListener(this);
        mDataBinding.ivStartSpeedReduce.setOnClickListener(this);
        mDataBinding.ivStartSpeedAdd.setOnClickListener(this);
        mDataBinding.ivMaxSpeedReduce.setOnClickListener(this);
        mDataBinding.ivMaxSpeedAdd.setOnClickListener(this);


        minuteList = new ArrayList<>();
        minuteStrList=new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            minuteList.add(i);
            minuteStrList.add(i + "");
        }

        WheelAdapter minuteAdapter = new ArrayWheelAdapter(minuteStrList);
        mDataBinding.wvMinute.setAdapter(minuteAdapter);

        mDataBinding.wvMinute.setTextColorCenter(Color.parseColor("#7B6FEE"));
        mDataBinding.wvMinute.setTextSize(24);
        mDataBinding.wvMinute.setDividerColor(Color.parseColor("#F1F0FF"));
        mDataBinding.wvMinute.setDividerType(WheelView.DividerType.FILL);
        mDataBinding.wvMinute.setItemsVisibleCount(3);
        mDataBinding.wvMinute.setCurrentItem(1);
        mDataBinding.wvMinute.setCyclic(false);

        secondList = new ArrayList<>();
        secondStrList=new ArrayList<>();
        for (int i = 1; i < 60; i++) {
            secondList.add(i);
            secondStrList.add(i + "");
        }

        WheelAdapter secondAdapter = new ArrayWheelAdapter(secondStrList);
        mDataBinding.wvSecond.setAdapter(secondAdapter);

        mDataBinding.wvSecond.setTextColorCenter(Color.parseColor("#7B6FEE"));
        mDataBinding.wvSecond.setTextSize(24);
        mDataBinding.wvSecond.setDividerColor(Color.parseColor("#F1F0FF"));
        mDataBinding.wvSecond.setDividerType(WheelView.DividerType.FILL);
        mDataBinding.wvSecond.setItemsVisibleCount(3);
        mDataBinding.wvSecond.setCurrentItem(1);
        mDataBinding.wvSecond.setCyclic(false);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvSettingCancel:
                dismiss();
                break;
            case R.id.tvSettingConfirm:
                dismiss();
                if (listener != null) {
                    int minute = minuteList.get(mDataBinding.wvMinute.getCurrentItem());
                    int second = secondList.get(mDataBinding.wvSecond.getCurrentItem());
                    int timing = minute * 60 + second;
                    listener.onClickConfirm(hasOpenFlash, hasOpenVibrate, hasSetTime, timing, timeInterval, addSpeed, startSpeed, maxSpeed);
                }
                break;
            case R.id.tvSetFlashLamp://闪光灯
                hasOpenFlash = !hasOpenFlash;
                mDataBinding.tvSetFlashLamp.setSelected(hasOpenFlash);
                break;
            case R.id.tvSetVibration://震动
                hasOpenVibrate = !hasOpenVibrate;
                mDataBinding.tvSetVibration.setSelected(hasOpenVibrate);
                break;
            case R.id.tvSetTime://定时设置
                hasSetTime = !hasSetTime;
                mDataBinding.tvSetTime.setSelected(hasSetTime);
                break;
            //时间间隔
            case R.id.ivMinuteReduce:
                if (timeInterval == 1) {
                    ToastUtils.showShort(R.string.no_zero_minute);
                    return;
                }
                timeInterval--;
                mDataBinding.tvMinute.setText(TimeUtil.getHHmmss(timeInterval * 60 * 1000));
                break;
            case R.id.ivMinuteAdd:
                timeInterval++;
                mDataBinding.tvMinute.setText(TimeUtil.getHHmmss(timeInterval * 60 * 1000));
                break;
            //每次增加的速度
            case R.id.ivSpeedReduce:
                if (addSpeed == 1) {
                    ToastUtils.showShort(R.string.speed_tips);
                    return;
                }
                addSpeed--;
                mDataBinding.tvSpeed.setText(addSpeed + "");
                break;
            case R.id.ivSpeedAdd:
                addSpeed++;
                mDataBinding.tvSpeed.setText(addSpeed + "");
                break;
            //开始速度
            case R.id.ivStartSpeedReduce:
                if (startSpeed == 20) {
                    ToastUtils.showShort(R.string.start_speed_tips);
                    return;
                }
                startSpeed--;
                mDataBinding.tvStartSpeed.setText(startSpeed + "");
                break;
            case R.id.ivStartSpeedAdd:
                if (startSpeed == 400) {
                    ToastUtils.showShort(R.string.max_speed_tip2);
                    return;
                }
                startSpeed++;
                mDataBinding.tvStartSpeed.setText(startSpeed + "");
                break;
            //最大速度
            case R.id.ivMaxSpeedReduce:
                if (maxSpeed == startSpeed) {
                    ToastUtils.showShort(R.string.max_speed_tip1);
                    return;
                }
                maxSpeed--;
                mDataBinding.tvMaxSpeed.setText(maxSpeed + "");
                break;
            case R.id.ivMaxSpeedAdd:
                if (maxSpeed == 400) {
                    ToastUtils.showShort(R.string.max_speed_tip2);
                    return;
                }
                maxSpeed++;
                mDataBinding.tvMaxSpeed.setText(maxSpeed + "");
                break;
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void onClickConfirm(boolean hasOpenFlash, boolean hasOpenVibrate, boolean hasSetTime, int timing, int timeInterval, int addSpeed, int startSpeed, int maxSpeed);
    }
}
